﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web;
using LDSP.BRL;

namespace LDSP.Util
{
    public class CodificationExportBase<T, V>
        where T : new()
        where V : new()
    {
        public IDictionary<string, DataTable> GetDataTables()
        {
            return GetDataTables(GetAllCatalogs());
        }

        public IDictionary<string, DataTable> GetDataTables(IList<T> catalogs)
        {
            IDictionary<string, DataTable> dataTables = new Dictionary<string, DataTable>();

            foreach (T cata in catalogs)
            {
                GenerateList(dataTables, cata);
            }

            return dataTables;
        }

        public void Export(IDictionary<string, DataTable> tables, HttpServerUtility server, string templateName)
        {
            //包含冒号的，是绝对路径；否则是相对路径。
            string filePath = templateName.Contains(":") ? templateName : server.MapPath("~\\Templates\\Codification\\" + templateName);
            ExcelClass excel = new ExcelClass(filePath);

            foreach (string key in tables.Keys)
            {
                excel.ImportDataTable(excel.GenerateSheet(key), false, tables[key], 2, 1);
            }

            //Remove the 1st sheet, because it is just a template.
            if (excel.Sheets.Count > 1)
            {
                excel.RemoveSheet(0);
            }

            excel.ToExcel(@"E:\Lab\Web\ExcelImportExport\CodificationList.xls", DownloadType.PromptDialog);
        }

        public DataTable ConstructDT(IList<V> codifications)
        {
            DataTable dt = new DataTable();
            InitialColumns(dt);
            InitialRows(dt, codifications);

            return dt;
        }

        protected virtual void InitialRows(DataTable dt, IList<V> codifications)
        {
            return;
        }

        protected virtual void InitialColumns(DataTable dt)
        {
            return;
        }

        protected virtual IList<T> GetAllCatalogs()
        {
            return null;
        }

        protected virtual void GenerateList(IDictionary<string, DataTable> dataTables, T cata)
        {
            return;
        }

        protected string GetFullName(Catalog cata)
        {
            string name = cata.Name;
            Catalog current = cata;
            while (current.ParentID != 0)
            {
                current = current.Parent;
                name = name.Insert(0, current.Name + "-");
            }

            return name;
        }

        protected string GenerateInvalidList(Codification cod)
        {
            string invalidList = string.Empty;
            List<R_DisableDoc> invalids = new R_DisableDoc().GetListBySource(cod.ID);
            if (invalids.Count > 0)
            {
                foreach (R_DisableDoc doc in invalids)
                {
                    invalidList += GetInvalidStr(doc.Destination);
                }
            }
            return invalidList;
        }

        protected virtual string GetInvalidStr(Codification doc)
        {
            return "《" + doc.Name + "》\n";
        }
    }
}
